package handle

import (
	"bytes"
	"encoding/json"
	"net/http"
	"net/http/httptest"
	"project-info/src/model"
	"testing"

	"github.com/gin-gonic/gin"
	"github.com/stretchr/testify/assert"
)

func UnitTestSetUp() *gin.Engine {
	model.ConfigInit("../../conf.toml")
	model.DbInit()

	e := gin.New()
	//e.POST("/category/add", CategoryAdd)
	//e.POST("/category/delete", CategoryDelete)
	//e.POST("/category/update", CategoryUpdate)
	//e.POST("/category/list", CategoryList)
	//
	//e.POST("/file/upload", UploadFile)
	//e.POST("/file/delete", DeleteFile)
	//e.POST("/changeFileName", ChangeFileName)
	//e.POST("/file/list", SelectCategoryFiles)
	return e
}

func HttpReq(method string, url string, marshal []byte, content string, e *gin.Engine) *httptest.ResponseRecorder {
	req, _ := http.NewRequest(method, url, bytes.NewBuffer(marshal))
	req.Header.Set("Content-Type", content)
	w := httptest.NewRecorder()
	e.ServeHTTP(w, req)
	return w
}

func HttpReqByBodyBuffer(method string, url string, buffer *bytes.Buffer, content string, e *gin.Engine) *httptest.ResponseRecorder {
	req, _ := http.NewRequest(method, url, buffer)
	req.Header.Set("Content-Type", content)
	w := httptest.NewRecorder()
	e.ServeHTTP(w, req)
	return w
}

func OkMsgValidateResult(t *testing.T, w *httptest.ResponseRecorder) {
	var response map[string]string
	json.Unmarshal([]byte(w.Body.String()), &response)
	msg, exists := response["msg"]
	assert.True(t, exists)
	assert.Equal(t, "ok", msg)
	assert.Equal(t, http.StatusOK, w.Code)
}
